﻿namespace Methods
{
    using System;

    class ExerciseWithTasksAndTextMenu
    {
        // 1.1.9.11. Write a program that solves the following tasks:
        // - Put the digits from an integer number into a reversed order.
        // - Calculate the average of given sequence of numbers.
        // - Solve the linear equation a * x + b = 0.
        // Create appropriate methods for each of the above tasks.
        // Make the program show a text menu to the user. By choosing an option of that menu,
        // the user will be able to choose which task to be invoked.
        // Perform validation of the input data:
        // - The integer number must be a positive in the range [1…50,000,000].
        // - The sequence of numbers cannot be empty.
        // - The coefficient a must be non-zero.

        public static double AverageNum(int countNums)
        {
            int[] nums = new int[countNums];
            for (int i = 0; i < countNums; i++)
            {
                Console.Write("Enter number {0}: ", i + 1);
                nums[i] = int.Parse(Console.ReadLine());
            }
            double average = 0;
            for (int i = 0; i < nums.Length; i++)
            {
                average += nums[i];
            }
            average /= nums.Length;


            return average;
        }
        public static double LinearEquation(double a, double b)
        {
            return (b * -1) / a;
        }

        static void Main()
        {
            Console.WriteLine("Menu:");
            Console.WriteLine("1. Reverse digit");
            Console.WriteLine("2. Average of numbers");
            Console.WriteLine("3. Solve a * x + b = 0");
            Console.Write("Pick 1, 2 or 3: ");
            string userChoice = Console.ReadLine();
            if (userChoice == "1")
            {
                Console.Write("Enter a number: ");
                int num = int.Parse(Console.ReadLine());
                while (num < 1 || num > 50000000)
                {
                    Console.WriteLine("Please enter number in the range [1, ..., 50 000 000]");
                    Console.Write("Enter a number: ");
                    num = int.Parse(Console.ReadLine());
                }

                Console.WriteLine(ReversingDigits.ReverseNumber(num));    // from 1.1.9.07
            }
            else if (userChoice == "2")
            {
                Console.Write("Enter the count of numbers to average: ");
                int countNums = int.Parse(Console.ReadLine());
                while (countNums < 1)
                {
                    Console.WriteLine("Please enter a count of number higher than 0");
                    Console.Write("Enter the count of numbers to average: ");
                    countNums = int.Parse(Console.ReadLine());
                }

                double result = AverageNum(countNums);
                Console.WriteLine("The average sum is: {0}", result);
            }
            else if (userChoice == "3")
            {
                Console.Write("Enter a: ");
                double a = double.Parse(Console.ReadLine());
                while (a == 0)
                {
                    Console.WriteLine("\"a\" must be non-zero");
                    Console.Write("Enter a: ");
                    a = double.Parse(Console.ReadLine());
                }
                Console.Write("Enter b: ");
                double b = int.Parse(Console.ReadLine());
                Console.WriteLine("x = {0:F2}", LinearEquation(a, b));
            }
            else
            {
                Console.WriteLine("Don't be a nerd.");
            }
        }
    }
}